In [75]:
import numpy as np
import math
In [249]:
class NeuralNet(object):
def __init__(self, layers, rows, col):
self.rows=rows
self.col=col
self.weights=np.empty((layers, rows, col))
self.bias=np.empty((layers, rows, 1))
for i in range(layers):
self.weights[i]=np.random.rand(rows, col)
self.bias[i]=np.random.rand(rows, 1)
def feedforward(self, train, layer, training_point):
return (np.dot(self.weights[layer], train.input[training_point])+self.bias)[0]
def sigmoid(self, train, layer, training_point):
output=self.feedforward(train, layer, training_point)
for i in range(train.dim):
output[i]=1/(1+math.exp(-output[i]))
return output
def error(self, train, layer, training_point):
return self.sigmoid(train, layer, training_point)-train.target[training_point]
def grad(self, train, layer, training_point):
for row in range(self.rows):
err=self.error( train, layer, training_point)[row]
sig=self.sigmoid( train, layer, training_point)[row]
for col in range(self.col):
self.weights[layer][row][col] -= .1*err*(sig)*(1-sig)*(train.input[layer][col])
In [247]:
class Trainer:
def __init__(self, max_points, dim):
self.dim=dim
self.training_points=0
self.input=np.empty((max_points, dim, 1))
self.target=np.empty((max_points, dim, 1))
def add_point(self, inp, tar):
self.input[self.training_points]=inp
self.target[self.training_points]=tar
self.training_points+=1
In [251]:
y=Trainer(1,2)
y.add_point(np.array(([5], [1])), np.array(([.99], [.01])))
x=NeuralNet(1,2,2)
for i in range(10000):
x.grad(y,0, 0)
print x.error(y, 0, 0)
In [ ]:
In [ ]:
In [ ]: